package joe.works.pluginframework.support.nshandler;

import joe.works.pluginframework.impl.PluginManager;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * BeanDefinitionParser for pluginManager tag.
 * <ol>
 * 	<li>&lt;pluginManager/&gt;</li>
 * 	<li>&lt;pluginManager resources="abc.xml, def.xml"/&gt;</li>
 * </ol>
 * 		
 * @author josseyj
 *
 */
public class PluginManagerBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser implements BeanDefinitionParser {

	public interface Attributes {
		String RESOURCES = "resources";
	}
	
	@Override
	protected Class getBeanClass(Element element) {
		return PluginManager.class;
	}
	
	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
		String resourcesCsv = element.getAttribute(Attributes.RESOURCES);
		if(resourcesCsv == null || resourcesCsv.isEmpty()) {
			resourcesCsv = "classpath:/**/*-plugin.xml";
		}
		builder.addPropertyValue("pluginResources", resourcesCsv.split("'"));
		builder.setInitMethodName("initialize");
	}
	
	@Override
	protected boolean shouldGenerateId() {
		return true;
	}

}